﻿using Client.Constant;
using Client.Framework.Event;
using Client.Framework.Message;
using Client.Framework.Socket;
using Client.Message.Request;
using Client.Message.Response;
using Client.Model;
using Client.View.PermissionView;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Client.ViewModel.PermissionViewModel
{
    /// <summary>
    /// Author: Leil
    /// Description: PermissionListViewModel
    /// CreateTime: 2020/5/28 19:06:45
    /// </summary>
    public class PermissionListViewModel : BindableBase
    {
        public PermissionListViewModel()
        {
            MsgEventSubscribe();
            Init();
        }

        /// <summary>
        /// 页面初始化
        /// </summary>
        private void Init()
        {
            AddPermissionCommand = new DelegateCommand(AddPermission);
            RefreshPermissionListCommand = new DelegateCommand(RefreshPermissionList);
            PrePageCommand = new DelegateCommand(PrePage);
            NextPageCommand = new DelegateCommand(NextPage);
            EditPermissionCommand = new DelegateCommand<ViewPermisson>(EditPermission);
            DeletePermissionCommand = new DelegateCommand<ViewPermisson>(DeletePermission);
            AllotPerUserCommand = new DelegateCommand<ViewPermisson>(AllotPerRole);
            PermissionEditWindow = new PermissionEdit();

            HasPrePage = false;
            HasNextPage = false;

            InitPermissions();
        }

        public void InitPermissions()
        {
            GetPermissions();
        }

        /// <summary>
        /// 消息事件订阅
        /// </summary>
        private void MsgEventSubscribe()
        {
            MessageEventPublisher.SubscribeEvent(MessageId.PERMISSION_LIST, PermissionResp);
        }

        /// <summary>
        /// 用户列表
        /// </summary>
        private List<ViewPermisson> permissons;
        public List<ViewPermisson> Permissions
        {
            get { return permissons; }
            set
            {
                permissons = value;
                RaisePropertyChanged("Permissions");
            }
        }


        /// <summary>
        /// 新增编辑页面
        /// </summary>
        public PermissionEdit PermissionEditWindow { get; private set; }

        /// <summary>
        /// 当前页码
        /// </summary>
        private int currentPage = 1;

        public int CurrentPage
        {
            get { return currentPage; }
            set { currentPage = value; RaisePropertyChanged("CurrentPage"); }
        }

        /// <summary>
        /// 每页条数
        /// </summary>
        private readonly int LIMIT = 10;

        /// <summary>
        /// 是否有上一页
        /// </summary>
        private bool hasPrePage;

        public bool HasPrePage
        {
            get { return hasPrePage; }
            set
            {
                hasPrePage = value;
                RaisePropertyChanged("HasPrePage");
            }
        }



        /// <summary>
        /// 是否有下一页
        /// </summary>
        private bool hasNextPage;

        public bool HasNextPage
        {
            get { return hasNextPage; }
            set
            {
                hasNextPage = value;
                RaisePropertyChanged("HasNextPage");
            }
        }


        /// <summary>
        /// 新增角色
        /// </summary>
        public DelegateCommand AddPermissionCommand { get; private set; }
        public void AddPermission()
        {
            PermissionEditWindow.BindParentContextAndEntity(this);
            PermissionEditWindow.ShowDialog();
        }

        /// <summary>
        /// 刷新角色列表
        /// </summary>
        public DelegateCommand RefreshPermissionListCommand { get; private set; }
        public void RefreshPermissionList()
        {
            GetPermissions();
        }

        /// <summary>
        /// 上一页
        /// </summary>
        public DelegateCommand PrePageCommand { get; private set; }
        public void PrePage()
        {
            if (hasPrePage)
            {
                CurrentPage--;
                GetPermissions();
            }
        }

        /// <summary>
        /// 下一页
        /// </summary>
        public DelegateCommand NextPageCommand { get; private set; }
        public void NextPage()
        {
            if (hasNextPage)
            {
                CurrentPage++;
                GetPermissions();
            }
        }

        /// <summary>
        /// 编辑角色信息
        /// </summary>
        public DelegateCommand<ViewPermisson> EditPermissionCommand { get; private set; }
        public void EditPermission(ViewPermisson permission)
        {
            PermissionEditWindow.BindParentContextAndEntity(this, permission);
            PermissionEditWindow.ShowDialog();
        }

        /// <summary>
        /// 删除角色信息
        /// </summary>
        public DelegateCommand<ViewPermisson> DeletePermissionCommand { get; private set; }
        public void DeletePermission(ViewPermisson permission)
        {
        }

        /// <summary>
        /// 编辑角色权限
        /// </summary>
        public DelegateCommand<ViewPermisson> AllotPerUserCommand { get; private set; }
        public void AllotPerRole(ViewPermisson permission)
        {
        }




        /// <summary>
        /// 获取权限列表
        /// </summary>
        public void GetPermissions()
        {
            PermissonListReq msg = new PermissonListReq(MessageId.REQUEST_PERMISSION_LIST)
            {
                Page = CurrentPage,
                Limit = LIMIT
            };
            ApplicationClient.Instance().Send(msg);
        }


        /// <summary>
        /// 列表信息返回
        /// </summary>
        /// <param name="message">返回的消息</param>
        private void PermissionResp(ResponseBaseMessage message)
        {
            if (message is PermissionListResp msg)
            {
                Permissions = msg.Permissions;
                HasPrePage = CurrentPage > 1;
                HasNextPage = CurrentPage < (int)Math.Ceiling((double)msg.Count / LIMIT);
            }
        }


    }
}
